﻿using MaterialDesignThemes.Wpf;
using Restaurant.Client.Controls;
using Restaurant.Client.Framework;
using Restaurant.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Restaurant.Client.Framework.Extension;
using Restaurant.Model;
using Restaurant.Model.ExModel;

namespace Restaurant.Client.Pages
{
    /// <summary>
    /// p_Setting_Food.xaml 的交互逻辑
    /// </summary>
    public partial class p_Settings_Food : Page
    {
        private bool _isAdd;

        private bool IsAdd
        {
            get => _isAdd;
            set
            {
                _isAdd = value;
                if (_isAdd)
                {
                    tb_Title.Text = "添加分类";
                }
                else
                {
                    tb_Title.Text = "修改分类";
                }
            }
        }

        public p_Settings_Food()
        {
            InitializeComponent();
            GetFood();
        }

        /// <summary>
        /// 获取分类
        /// </summary>
        private async void GetFood()
        {
            var s = new Dictionary<string, object>();
            s.Add("subId", Global.SubCompanyId);
            var r = await APIHelper.TryGetAsync<OperationResult>("Food/GetAllAsync", s);
            var items = r.GetListAppData<FoodType_Food_Ex>();

            foreach (var item in items.Item1)
            {
                u_FoodType t = new u_FoodType(item);
                t.Edit += T_Edit;
                t.Delete += T_Delete;
                wp_Main.Children.Add(t);
            }
        }


        #region 删除

        /// <summary>
        /// 子元素删除
        /// </summary>
        /// <param name="sender"></param>
        private async void T_Delete(u_FoodType sender)
        {
            //删除数据
            var s = new Dictionary<string, object>();
            s.Add("id", sender.FoodType_Food_Ex.Id);

            var r = await APIHelper.TryDeleteAsync<OperationResult>("Food/DeleteAsync", s);

            if (r.ResultType == OperationResultType.Success)

                //删除显示
                wp_Main.Children.Remove(sender);
        }

        #endregion


        #region 添加 修改

        private void dh_Food_Setting_DialogClosing(object sender, DialogClosingEventArgs eventArgs)
        {
            try
            {
                //取消判断
                if (!Equals(eventArgs.Parameter, true)) return;

                //保存数据   界面显示
                if (IsAdd)
                {

                    Add();
                }
                else
                {
                    Update();
                }
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                txt_Add_Name.Text = "";
                txt_Add_Remarks.Text = "";
            }
        }


        /// <summary>
        /// 分类编辑
        /// </summary>
        /// <param name="sender"></param>
        private void T_Edit(u_FoodType sender)
        {
            IsAdd = false;
            txt_Add_Name.Text = sender.FoodType_Food_Ex.FoodTypeName;
            txt_Add_Remarks.Text = sender.FoodType_Food_Ex.Remarks;
            DialogHost.OpenDialogCommand.Execute(null, sender);
        }
        private async void Update()
        {

        }

        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Food_Add_Click(object sender, RoutedEventArgs e)
        {
            IsAdd = true;
        }

        private async void Add()
        {
            FoodType_Food_Ex foodType = new FoodType_Food_Ex();
            foodType.SubCompany_Id = Global.SubCompanyId;
            foodType.FoodTypeName = txt_Add_Name.Text.Trim();
            foodType.Remarks = txt_Add_Remarks.Text.Trim();
            foodType.Operator = Global.Oprator;

            var r = await APIHelper.TryPostAsync<OperationResult>("Food/AddAsync", foodType);
            if (r.ResultType == OperationResultType.Success)
            {
                foodType = r.GetAppData<FoodType_Food_Ex>();

                u_FoodType t = new u_FoodType(foodType);
                t.Edit += T_Edit;
                t.Delete += T_Delete;
                wp_Main.Children.Add(t);
            }
        }

        #endregion

    }
}
